package com.byxy.action.department;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * Servlet implementation class DepartmentDelServlet
 */
@WebServlet("/DepartmentDelServlet")
public class DepartmentDelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DepartmentDelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {

			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // �������ݿ�������ע�ᵽ����������
			String url = "jdbc:sqlserver://127.0.0.1:1433;databasename=PartyMember";// ���ݿ������ַ���
			String username = "sa"; // ���ݿ��û���
			String password = "123456"; // ���ݿ�����
			// ����Connection����
			Connection conn = DriverManager.getConnection(url, username, password);
			String sql = "DELETE FROM Department WHERE id=?";// ����SQL���
			PreparedStatement ps = conn.prepareStatement(sql);// ��ȡPreparedStatement
			String ID[] = request.getParameterValues("delid"); // ��ȡҪɾ���ı��
			if (ID.length > 0) {
				for (int i = 0; i < ID.length; i++) {
					ps.setInt(1, Integer.parseInt(ID[i])); // ��SQL����еĵ�1��������ֵ
					ps.addBatch(); // �������������
				}
			}
			ps.executeBatch(); // ִ�����������
			ps.close(); // �ر�PreparedStatement
			conn.close(); // �ر�Connection
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.sendRedirect("DepartmentFindAllServlet"); // �ض���FindServlet
	}

}
